package com.sherllinhe.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sherllinhe.common.context.LoginMemberContext;
import com.sherllinhe.business.domain.*;
import com.sherllinhe.business.req.TrainStationQueryReq;
import com.sherllinhe.business.resp.TrainStationQueryResp;
import com.sherllinhe.common.exception.BusinessException;
import com.sherllinhe.common.exception.BusinessExceptionEnum;
import com.sherllinhe.common.resp.PageResp;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.business.mapper.TrainMapper;
import com.sherllinhe.business.req.TrainQueryReq;
import com.sherllinhe.business.req.TrainSaveReq;
import com.sherllinhe.business.resp.TrainQueryResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/5/7 16:48
 */
@Slf4j
@Service
public class TrainService {
    @Autowired
    private TrainMapper trainMapper;
    // 新增和编辑乘客
    public void save(TrainSaveReq req){
        DateTime now = DateTime.now();
        Train train = BeanUtil.copyProperties(req, Train.class);

        //判断是新增还是更新
        //train.setMemberId(LoginMemberContext.getId());
        if(ObjUtil.isNull(train.getId())) {
            //新增前查询唯一键是否存在
            Train trainDB = selectByUnique(train.getCode());
            if(ObjUtil.isNotNull(trainDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
            }
            //新增
            train.setId(SnowUtil.getSnowflakeNextId());
            train.setCreateTime(now);
            train.setUpdateTime(now);
            trainMapper.insert(train);
        }else{
            //更新
            train.setUpdateTime(now);
            trainMapper.updateByPrimaryKey(train);
        }


    }
    // 分页查询火车列表
    public PageResp<TrainQueryResp> queryList(TrainQueryReq req){
        TrainExample example = new TrainExample();
        example.setOrderByClause("id asc");
        TrainExample.Criteria criteria = example.createCriteria();
        //if(ObjUtil.isNotNull(req.getMemberId())){
            //criteria.andMemberIdEqualTo(req.getMemberId());
        //}

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Train> trainList = trainMapper.selectByExample(example);

        PageInfo<Train> pageInfo = new PageInfo<>(trainList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainQueryResp> list = BeanUtil.copyToList(trainList, TrainQueryResp.class);

        PageResp<TrainQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //删除乘客
    public void delete(Long id){
        trainMapper.deleteByPrimaryKey(id);
    }

    // 查询火车列表全部
//    @Transactional
    public List<TrainQueryResp> queryAll(){
        List<Train> trainList = selectAll();
//        log.info("再查一次");
//        trainList = selectAll();
        return BeanUtil.copyToList(trainList, TrainQueryResp.class);
    }

    public List<Train> selectAll() {
        TrainExample example = new TrainExample();
        example.setOrderByClause("code asc");
        List<Train> trainList = trainMapper.selectByExample(example);
        return trainList;
    }

    //  查询唯一键
    private Train selectByUnique(String code) {
        TrainExample example = new TrainExample();
        example.createCriteria().andCodeEqualTo(code);
        List<Train> list = trainMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else {
            return null;
        }
    }
    //根据车次编号查询信息
    public List<TrainQueryResp> queryByCode(String code){
        TrainExample example = new TrainExample();
        example.createCriteria().andCodeEqualTo(code);
        List<Train> list = trainMapper.selectByExample(example);
        return BeanUtil.copyToList(list, TrainQueryResp.class);
    }
}
